from typing import *


# Definition for a binary tree node.
class TreeNode:

    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


class Solution:

    def rob(self, root: Optional[TreeNode]) -> int:

        def f(root: Optional[TreeNode]) -> []:
            if root is None:
                return [0, 0]
            l, r = f(root.left), f(root.right)
            a, b = root.val + l[0] + r[0], l[1] + r[1]

            return [b, max(a, b)]
        
        return f(root)[1]